package com.journey.org.ui.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.ViewGroup
import android.webkit.WebSettings
import com.github.lzyzsd.jsbridge.DefaultHandler
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.FragmentMineBinding
import com.mvvmhabit.org.base.BaseLazyFragment

/**
 * @Date: 2020/11/17 14:22
 * @Author:小妖
 * @Description: 个人中心
 */
class MineFragment : BaseLazyFragment<FragmentMineBinding, MineViewModel>() {

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<MineViewModel> = MineViewModel::class.java

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.fragment_mine

    override fun lazyLoadData() {

    }

    override fun initData() {
        super.initData()
        mBinding.layoutMineWebView.setDefaultHandler(DefaultHandler())
        mBinding.layoutMineWebView.loadUrl("file:///android_asset/index.html")
        /*     mBinding.btnTest.setOnClickListener {
                 // mBinding.tvCustom.smoothScrollTo(-400, -400)
                 val mObjectAnimator =
                     ObjectAnimator.ofFloat(mBinding.tvCustom, "translationX", 400F)
                 mObjectAnimator.duration = 2000
                 mObjectAnimator.start()
             }*/
        initWebSetting()

    }

    // 初始化webView 的辅助类
    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebSetting() {
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        mBinding.layoutMineWebView.settings.javaScriptEnabled = true
        //支持插件
        // webSettings.setPluginsEnabled(true);
        //设置自适应屏幕，两者合用
        mBinding.layoutMineWebView.settings.useWideViewPort = true //将图片调整到适合webview的大小
        mBinding.layoutMineWebView.settings.loadWithOverviewMode = true // 缩放至屏幕的大小
        //缩放操作
        mBinding.layoutMineWebView.settings.setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
        mBinding.layoutMineWebView.settings.builtInZoomControls =
            true //设置内置的缩放控件。若为false，则该WebView不可缩放
        mBinding.layoutMineWebView.settings.displayZoomControls = false //隐藏原生的缩放控件
        //其他细节操作
        mBinding.layoutMineWebView.settings.cacheMode =
            WebSettings.LOAD_CACHE_ELSE_NETWORK //关闭webview中缓存
        mBinding.layoutMineWebView.settings.allowFileAccess = true //设置可以访问文件
        mBinding.layoutMineWebView.settings.javaScriptCanOpenWindowsAutomatically =
            true //支持通过JS打开新窗口
        mBinding.layoutMineWebView.settings.loadsImagesAutomatically = true //支持自动加载图片
        mBinding.layoutMineWebView.settings.defaultTextEncodingName = "utf-8" //设置编码格式
        // 对于android WebView加载不出Html5网页的解决方法
        mBinding.layoutMineWebView.settings.domStorageEnabled = true
    }

    override fun onDestroyView() {
        super.onDestroyView()
        println("MineFragment结束当前进程：：onDestroyView")
    }

    /*  //点击返回上一页面而不是退出浏览器
      fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
          if (keyCode == KeyEvent.KEYCODE_BACK && mBinding.layoutMineWebView.canGoBack()) {
              mBinding.layoutMineWebView.goBack()
              return true
          }
          return super.onKeyDown(keyCode, event)
      }
      */

    //销毁Webview
    override fun onDestroy() {
        if (mBinding.layoutMineWebView != null) {
            mBinding.layoutMineWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
            mBinding.layoutMineWebView.clearHistory()
            (mBinding.layoutMineWebView.getParent() as ViewGroup).removeView(mBinding.layoutMineWebView)
            mBinding.layoutMineWebView.destroy()
        }
        super.onDestroy()
    }

}